package com.wangsd.web.sys.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.component.WxApiV3CouponService;
import com.wangsd.web.sys.entity.SysCompany;
import com.wangsd.web.sys.entity.SysUser;
import com.wangsd.web.sys.service.SysCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "服务商管理")
@RestController
@RequestMapping("/sys/company")
@Slf4j
public class SysCompanyController extends MyController {
    @Resource
    private SysCompanyService service;

    @ApiOperation(value = "分页查询服务商列表")
    @PostMapping("/selectPage")
    public Object selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                             @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                             SysCompany queryObj) {
        SysUser loginUser = getUserInfo();
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<SysCompany> list = service.selectPage(queryObj, loginUser.getParentCode());
        return Result.success(getPageInfo(page, list));
    }


    @ApiOperation(value = "删除服务商")
    @GetMapping("/{id}/delete")
    public Result delete(@ApiParam(value = "id", required = true) @PathVariable() Long id) {
        service.deleteById(id);
        return Result.success();
    }


    @ApiOperation(value = "新增或者修改服务商")
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(@RequestBody SysCompany entity) {
        service.saveOrUpdate(entity);
        return Result.success();
    }

    @ApiOperation(value = "查询所有服务商（下拉列表）")
    @PostMapping("/selectAllCompany")
    public Result selectAllCompany(@ApiParam(value = "类型") @RequestParam(value = "type", required = false) Integer type) {
        SysUser loginUser = getUserInfo();
        List<SysCompany> list = service.selectAllCompany(loginUser.getParentCode(), type);
        return Result.success(list);
    }

    @ApiOperation(value = "查询树形结构")
    @PostMapping("/selectAgentTree")
    public Object selectAgentTree() {
        SysUser loginUser = getUserInfo();
        List list = service.selectAgentTree(loginUser.getParentCode());
        return Result.success(list);
    }

    @ApiOperation(value = "设置优惠券回调地址")
    @PostMapping("/updateNotifyUrl")
    public Object updateNotifyUrl(@ApiParam(value = "id", required = true) @RequestParam Long id,
                                  @ApiParam(value = "回调地址", required = true) @RequestParam String notifyUrl,
                                  @ApiParam(value = "回调开关", required = true) @RequestParam Boolean isSwitch) {
        service.updateNotifyUrl(id, notifyUrl, isSwitch);
        return Result.success();
    }

    @ApiOperation(value = "查询商家券回调地址")
    @GetMapping("/{id}/getCallBack")
    public Result getCallBack(@ApiParam(value = "id", required = true) @PathVariable() Long id) {
        SysCompany company = service.selectById(id);
        WxApiV3CouponService wxApiV3CouponService = new WxApiV3CouponService(company);
        String str = wxApiV3CouponService.getCallBack();
        return Result.success(JSON.parseObject(str));
    }

    @ApiOperation(value = "设置商家券回调地址")
    @PostMapping("/setCallBack")
    public Object setCallBack(@ApiParam(value = "id", required = true) @RequestParam Long id,
                                  @ApiParam(value = "回调地址", required = true) @RequestParam String merNotifyUrl) {
        SysCompany company = service.selectById(id);
        WxApiV3CouponService wxApiV3CouponService = new WxApiV3CouponService(company);
        String str = wxApiV3CouponService.setCallBack(merNotifyUrl);
        return Result.success(JSON.parseObject(str));
    }
}
